package cn.campusshop.article.service;

import cn.campusshop.article.dao.ArticleDetailDao;
import cn.campusshop.article.entity.ArticleDetail;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Lang;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhulang
 * @Classname ArticleDetailService
 * @Description ArticleDetailService
 */
@Service
public class ArticleDetailService {
    @Autowired
    private ArticleDetailDao articleDetailDao;

    public ArticleDetail findById(int id) {
        return articleDetailDao.selectById(id);
    }

    public List<ArticleDetail> findAll() {
        return articleDetailDao.selectList(null);
    }

    public Page findAllByPage(int page, int size) {
        Page<ArticleDetail> detailPage = new Page<>(page, size);
        QueryWrapper<ArticleDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("total_pv", "share_count");
        return articleDetailDao.selectPage(detailPage, queryWrapper);
    }

    public Page findAllByPageAndRefDate(int page, int size, String date) {
        Page<ArticleDetail> detailPage = new Page<>(page, size);
        QueryWrapper<ArticleDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.apply(!StringUtils.isEmpty(date), "date_format (ref_date,'%Y-%m')  = '" + date + "'");
        queryWrapper.orderByDesc("total_pv", "share_count");
        return articleDetailDao.selectPage(detailPage, queryWrapper);
    }

    public List<ArticleDetail> findAllByDate(String date) {
        QueryWrapper<ArticleDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.apply(!StringUtils.isEmpty(date), "date_format (ref_date,'%Y-%m')  = '" + date + "'");
        queryWrapper.orderByDesc("total_pv", "share_count");
        return articleDetailDao.selectList(queryWrapper);
    }

    public Page searchByTitle(int page, int size, String keyword) {
        Page<ArticleDetail> scorePage = new Page<>(page, size);
        QueryWrapper<ArticleDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(keyword), "title", keyword).orderByDesc("total_pv", "share_count");
        return articleDetailDao.selectPage(scorePage, queryWrapper);
    }

    public ArticleDetail findByTitle(String title) {
        QueryWrapper<ArticleDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(!StringUtils.isEmpty("title"), "title", title);
        return articleDetailDao.selectOne(queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    public int update(ArticleDetail articleDetail) {
        return articleDetailDao.updateById(articleDetail);
    }

    /**
     * 查询指定月份浏览量排名
     *
     * @param num  前num名
     * @param date 年月
     * @return
     */
    public List<ArticleDetail> findTopByPvAndDate(int num, String date) {
//select * from tb_article_detail tad where (date_format(ref_date,'%Y-%m')='2020-12') ORDER BY tad.total_pv DESC LIMIT 6;
        QueryWrapper<ArticleDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.apply(!StringUtils.isEmpty(date), "date_format (ref_date,'%Y-%m')  = '" + date + "'");
        Page<ArticleDetail> page = new Page<>(1, num);
        return articleDetailDao.selectPage(page, queryWrapper).getRecords();
    }

    /**
     * 查询指定月份分享量排名
     *
     * @param num  前num名
     * @param date 年月
     * @return
     */
    public List<ArticleDetail> findTopByShareAndDate(int num, String date) {
//select * from tb_article_detail tad where (date_format(ref_date,'%Y-%m')='2020-12') ORDER BY tad.total_pv DESC LIMIT 6;
        QueryWrapper<ArticleDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.apply(!StringUtils.isEmpty(date), "date_format (ref_date,'%Y-%m')  = '" + date + "'");
        queryWrapper.orderByDesc("share_count", "total_pv");
        Page<ArticleDetail> page = new Page<>(1, num);
        return articleDetailDao.selectPage(page, queryWrapper).getRecords();
    }

    /**
     * 查询指定日期范围的文章数量
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public Map<String, Long> findTotalCountByDate(String startDate, String endDate) {
        //SELECT  date_format(ref_date,'%Y-%m') , COUNT(1) c from tb_article_detail tad  WHERE  date_format(ref_date,'%Y-%m')  BETWEEN "2020-06" AND "2021-12" GROUP BY date_format(ref_date,'%Y-%m') ORDER BY  date_format(ref_date,'%Y-%m');
        QueryWrapper<ArticleDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("date_format(ref_date,'%Y-%m') as yearmonth , COUNT(1) as articlenum");
        queryWrapper.between("date_format(ref_date,'%Y-%m')", startDate, endDate);
        queryWrapper.groupBy("date_format(ref_date,'%Y-%m')").orderByAsc("date_format(ref_date,'%Y-%m')");
        List<Map<String, Object>> mapsList = articleDetailDao.selectMaps(queryWrapper);
        if (mapsList == null) {
            return null;
        }
        Map<String, Long> hashMap = new HashMap<>(mapsList.size());
        for (Map<String, Object> objectMap : mapsList) {
            hashMap.put((String) objectMap.get("yearmonth"), (Long) objectMap.get("articlenum"));
        }
        return hashMap;
    }
}
